<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT turn, turns_inactive FROM game";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$current_turn = $row['turn'];
$turns_to_inactive = $row['turns_inactive'];

$query = "SELECT MAX(x), MAX(y) FROM village";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);

$width = $row['MAX(x)'];
$height = $row['MAX(y)'];
//print( "width: " . $width . ", height: " . $height . "<br />" );
$map = Array ();

$query = "SELECT x, y " .
		"FROM village " .
		"WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND start_place = 1 LIMIT 1";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);

// TODO adjust this for performance and usability
$display = 40;

if (isset ($_GET['x'])) {
	$start_x = formatInput($_GET['x']);
} elseif (isset ($row['x'])) {
		$start_x = $row['x'] - $display / 2;
	} else {
		$start_x = 0;
	}

if ($start_x < 0) {
	$start_x = 0;
} elseif ($start_x > $width - $display) {
		if ($width > $display) {
			$start_x = $width - $display;
		} else {
			$start_x = 0;
		}
	}

$max_x = $start_x + $display;

if (isset ($_GET['y'])) {
	//print( "Y has been set to: " . $_GET[ 'y' ] . "<br />" );
	$start_y = formatInput($_GET['y']);
} elseif (isset ($row['y'])) {
		//print( "Y has row is: " . $row[ 'y' ] . "<br />" );
		$start_y = $row['y'] - $display / 2;
	} else {
		//print( "Y is: 0<br />" );
		$start_y = 0;
	}

//print( "start_y = $start_y<br />" );

if ($start_y < 0) {
	$start_y = 0;
} elseif ($start_y > $height - $display) {
		//print( "( start_y = $start_y ) > ( height = $height ) - ( display = $display )<br />" );
		if ($height > $display) {
			$start_y = $height - $display;
		} else {
			$start_y = 0;
		}
	}

$max_y = $start_y + $display;

//print( "start_y: " . $start_y . ", max_y: " . $max_y . "<br />" );
for ($i = $start_y; $i < $max_y; $i++) {
	for ($p = $start_x; $p < $max_x; $p++) {
		$map[$i * $display + $p] = "";
	}
}

$query = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND invited = 0 AND requested = 0";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$faction = false;
if (isset ($row['faction_id'])) {
	$faction = $row['faction_id'];
	$query = "SELECT scouted.village_id, village.ruler_id " .
	"FROM scouted " .
	"JOIN village ON scouted.village_id = village.village_id " .
	"JOIN faction_member ON faction_member.ruler_id = scouted.ruler_id " .
	"WHERE x >= $start_x AND x <= $max_x AND y >= $start_y AND y <= $max_y AND faction_id = " . $row['faction_id'] . " AND invited = 0 AND requested = 0";
} else {
	$query = "SELECT scouted.village_id, village.ruler_id FROM scouted JOIN village ON" .
	" scouted.village_id = village.village_id" .
	" WHERE scouted.ruler_id = " . $_SESSION['ruler_id'] . " AND x >= $start_x AND x <= $max_x AND y >= $start_y AND y <= $max_y";
}
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
	$faction_tag = ' ';
	if (mysql_result($result, $i, 'ruler_id') > -1) {
		$query2 = "SELECT active_turn FROM ruler WHERE ruler_id = " . mysql_result($result, $i, 'ruler_id');
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		$tag = "h";
		if ($row2['active_turn'] + $turns_to_inactive < $current_turn) {
			$inactive = "inactive";
		} else {
			$inactive = ' ';
		}
		$query2 = "SELECT faction_id " .
			"FROM faction_member " .
			"WHERE ruler_id = " . mysql_result($result, $i, 'ruler_id') . " AND invited = 0 AND requested = 0";
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		if (isset ($row2['faction_id'])) {
			$faction_tag = 'faction-color-' . $row2['faction_id'];
		}
	}
	elseif (mysql_result($result, $i, "ruler_id") == -1) {
		$tag = "i";
		$inactive = ' ';
	}

	$query2 = "SELECT x, y, start_place FROM village WHERE village_id = " . mysql_result($result, $i, "village_id");
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result2, MYSQL_ASSOC);
	if ($row['start_place'] == 1) {
		$tag .= "c";
	}

	$map[$row['x'] + $row['y'] * $display] = ($row['x'] + $row['y'] * ($width +1)) . "@$faction_tag@$inactive@$tag";
	$i++;
}

$query2 = "SELECT faction_id FROM faction_member " .
"WHERE ruler_id = " . $_SESSION['ruler_id'] . " " .
"AND invited = 0 AND requested = 0";
$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result2, MYSQL_ASSOC);
if (isset ($row['faction_id'])) {
	$query3 = "SELECT ruler_id FROM faction_member WHERE faction_id = " . $row['faction_id'] . " AND invited = 0 AND requested = 0";
	$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
	$num3 = mysql_numrows($result3);
	$p = 0;
	while ($p < $num3) {
		if (mysql_result($result3, $p, "ruler_id") != $_SESSION['ruler_id']) {
			//			    $query2 = "SELECT name FROM ruler WHERE ruler_id = " . mysql_result( $result3, $p, "ruler_id" );
			//			    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			//			    $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
			//			    $owner = $row[ 'name' ];
			$query2 = "SELECT active_turn FROM ruler WHERE ruler_id = " . mysql_result($result3, $p, 'ruler_id');
			$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			$tag = "a";
			if ($row2['active_turn'] + $turns_to_inactive < $current_turn) {
				$inactive = 'inactive';
			} else {
				$inactive = ' ';
			}

			$query = "SELECT x, y, start_place FROM village WHERE ruler_id = " . mysql_result($result3, $p, "ruler_id") . " AND x >= $start_x AND x <= $max_x AND y >= $start_y AND y <= $max_y";
			$result = mysql_query($query) or die("Query failed : " . mysql_error());
			$num = mysql_numrows($result);
			$i = 0;
			while ($i < $num) {
				if (mysql_result($result, $i, "start_place") == 1) {
					$tagadd = "c";
				} else {
					$tagadd = "";
				}
				$map[mysql_result($result, $i, "x") + mysql_result($result, $i, "y") * $display] = (mysql_result($result, $i, "x") + mysql_result($result, $i, "y") * ($width +1)) . "@ @$inactive@$tag$tagadd";
				$i++;
			}
		}
		$p++;
	}
}

//    $query2 = "SELECT name FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
//    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
//    $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
//    $owner = $row[ 'name' ];
$query = "SELECT x, y, start_place " .
		"FROM village " .
		"WHERE ruler_id = " . $_SESSION['ruler_id'] . " " .
		"AND x >= $start_x AND x <= $max_x AND y >= $start_y AND y <= $max_y";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
	$tag = "o";
	if (mysql_result($result, $i, "start_place") == 1) {
		$tag .= "c";
	}
	$map[mysql_result($result, $i, "x") + mysql_result($result, $i, "y") * $display] = (mysql_result($result, $i, "x") + mysql_result($result, $i, "y") * ($width +1)) . "@ @ @$tag";
	$i++;
}

for ($i = $start_y; $i <= $max_y; $i++) {
	for ($p = $start_x; $p <= $max_x; $p++) {
		if (isset ($map[$i * $display + $p]) && $map[$i * $display + $p] != '') {
			print ($map[$i * $display + $p] . "@");
		}
	}
}

print ('armies/');
if ($faction) {
	$query = "SELECT actual_x, actual_y, army.army_id, army.ruler_id, army.travel, scout, infantry, cavalry, artillery, bombard, fortified " .
		"FROM army_visible " .
		"JOIN army ON army.army_id = army_visible.army_id " .
		"WHERE army_visible.ruler_id IN ( SELECT faction_member.ruler_id FROM faction_member WHERE faction_id = $faction AND invited = 0 AND requested = 0 ) " .
		"AND actual_x >= $start_x AND actual_x <= $max_x AND actual_y >= $start_y AND actual_y <= $max_y ORDER BY bombard DESC, name";
} else {
	$query = "SELECT actual_x, actual_y, army.army_id, army.ruler_id, army.travel, scout, infantry, cavalry, artillery, bombard, fortified " .
		"FROM army_visible " .
		"JOIN army ON army.army_id = army_visible.army_id " .
		"WHERE army_visible.ruler_id = " . $_SESSION['ruler_id'] . " " .
		"AND actual_x >= $start_x AND actual_x <= $max_x AND actual_y >= $start_y AND actual_y <= $max_y ORDER BY bombard DESC, name";
}
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
	if (mysql_result($result, $i, "scout") + mysql_result($result, $i, "infantry") + mysql_result($result, $i, "cavalry") + mysql_result($result, $i, "artillery") > 0) {
		$moving = '';
		if (mysql_result($result, $i, "travel") > 0) {
			$moving = ' moving';
		}
		$fortified = '';
		if (mysql_result($result, $i, "fortified") > 0) {
			$fortified = ' fortified';
		}
		if (mysql_result($result, $i, "bombard") > 0) {
			$fortified .= ' bombard';
		}
		$faction_tag = '';

		if ($_SESSION['ruler_id'] == mysql_result($result, $i, "ruler_id")) {
			$tag = 'own';
		}
		elseif(mysql_result($result, $i, 'ruler_id' ) == -1 ) {
			$tag = 'hostile';
		}
		else {
			$query2 = "SELECT faction_id " .
					"FROM faction_member " .
					"WHERE ruler_id = " . mysql_result($result, $i, 'ruler_id') . " AND requested = 0 AND invited = 0";
			$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			if (isset ($row2['faction_id']) && $row2['faction_id'] == $faction) {
				$tag = 'allied';
			} else {
				$tag = 'hostile';
				if (isset ($row2['faction_id'])) {
					$faction_tag = ' faction-color-' . $row2['faction_id'];
				}
			}
		}

		print (mysql_result($result, $i, "army_id") . "|" . ((floor(mysql_result($result, $i, "actual_x") + 0.5) + floor(mysql_result($result, $i, "actual_y") + 0.5) * ($width +1))) . "|$tag$moving$fortified$faction_tag/");
	}
	$i++;
}

mysql_free_result($result);
mysql_close($link);
?>